<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$aWhere = array();
if (reqVar('pagifilt')!='') {
	$aWhere[] = " (a.rag_soc LIKE '%".addslashes(reqVar('pagifilt'))."%' "
		." OR a.cod_cciaa LIKE '%".addslashes(reqVar('pagifilt'))."%' "
		." OR a.cod_fisc LIKE '%".addslashes(reqVar('pagifilt'))."%') ";
}
$sWhere = (count($aWhere)==0)? "":" WHERE ".implode(" AND ",$aWhere);
$sql_sel = "SELECT count(*) AS num_rec " .
		" FROM rgl_azienda a " .$sWhere;
//dbg_($sql_sel);
$objDB->query($sql_sel,'selRec');
$acRec = $objDB->fetch('selRec');
$numRec = $acRec['num_rec'];
$link = $_SERVER['PHP_SELF']."?".$sXSNQS;
$startOffset = 0;
$endOffset = 0;
$sFiltro = rglGetFiltro($link);
$sPaginaz = rglGetPaginaz($link, $numRec, $startOffset, $endOffset);

$sql_sel = "SELECT a.*" .
		" FROM rgl_azienda a " .
		" ORDER BY a.rag_soc ".
		" LIMIT ".$startOffset.",".($endOffset-$startOffset);
$objDB->query($sql_sel,'selRec');
$cmdL = "";
$cmdR = "&nbsp;&nbsp;&nbsp;";
if (true) {
	$cmdR .= "<a class=\"cmd1\" href=\"anagIns.php?$sXSNQS\">nuova anagrafica</a>";
	$cmdR .= "&nbsp;&nbsp;&nbsp;";
	$cmdR .= "<a class=\"cmd1\" href=\"xbrl01BilIns.php?$sXSNQS\">carica bilanci da file XBRL</a>";
}
$cmdR .= cmdTorna(); 
testaHtml('', $msgPag, 'ratg');
echo getHtmlForm();
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('startoffset');
?>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
			<?=$sFiltro?>
      </td>
      <td class="cmd1" align="right">
			<?=$sPaginaz?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="tit1" colspan='6' align='center'>Lista Aziende</td>
   </tr>
   <tr>
      <td class="lab1" width='1%' align='center'>ID</td>
      <td class="lab1" width='9%' align='center'>Cod.CCIAA<br/>Cod.fiscale</td>
      <td class="lab1" width='30%' align='center'>Ragione sociale<br>Residenza</td>
      <td class="lab1" width='10%' align='center'>Settore</td>
      <td class="lab1" width='25%' align='center'>Bilanci</td>
      <td class="lab1" width='25%' align='center'>Rating</td>
   </tr>
			<? 
$clRF = 'txt2';
while($acX = $objDB->fetch('selRec')) { 
	$clRF = ($clRF=='txt1')?'txt2':'txt1';	
	$sSql = "SELECT b.id_bil, b.data_rif, b.descr, b.stato_lav, s.cod_set_dati "
		." FROM rgl_bilancio b "
		." LEFT JOIN rgl_set_dati s ON s.id_set_dati = b.id_set_dati "
		." WHERE b.id_azienda = ".$acX['id_azienda']
		." ORDER BY b.data_rif";
	$objDB->query($sSql,'lisBilRat');
	$sLisBil = '';
	while($aB = $objDB->fetch('lisBilRat')) {
		$sLisBil .= "<a href='bilCeeVis.php?id=".$aB['id_bil']."&$sXSNQS' "
			." class='$clRF'>".substr($aB['data_rif'],0,4)." - "
			.(($aB['descr']=='')?$aB['cod_set_dati']:substr($aB['descr'],0,25))
			.(($aB['stato_lav']=='verificato')?'':'&nbsp;(*)')."</a><br>";
	}
	$sSql = "SELECT r.*, m.cod_mod_rglob "
		." FROM rgl_rating r "
		." LEFT JOIN rgl_mod_rglob m ON m.id_mod_rglob = r.id_mod_rglob "
		." LEFT JOIN rgl_ratbil rb ON rb.id_rating = r.id_rating "
		." WHERE r.id_azienda = ".$acX['id_azienda']
		." ORDER BY r.data_rif";
	$objDB->query($sSql,'lisBilRat');
	$sLisRat = '';
	while($aB = $objDB->fetch('lisBilRat')) {
		$sLisRat .= "<a href='../crat/rglobVis.php?id=".$aB['id_rating']."&$sXSNQS' "
			." class='$clRF'>".YMD2DMY($aB['data_rif'])." (".$aB['sigla'].") - "
			.$aB['cod_mod_rglob']."</a><br>";
	}
	?>
   <tr>
      <td class="<?=$clRF?>" valign='top' align='right'>
      	<a href='anagVis.php?id=<?=$acX['id_azienda']."&$sXSNQS"?>' class="<?=$clRF?>"
      	 	style='font-family:lucida console,courier;'>
            <? echo $acX['id_azienda']; ?></a>&nbsp;</td>
      <td class="<?=$clRF?>" valign='top'>
      	<a href='anagVis.php?id=<?=$acX['id_azienda']."&$sXSNQS"?>' class="<?=$clRF?>"
      	 	style='font-family:lucida console,courier;'>
            <?
            echo $acX['cod_cciaa'];
            echo '<br/>';
            echo $acX['cod_fisc'];
            ?>
         </a>
      </td>
      <td class="<?=$clRF?>" align='left' valign='top'><? echo $acX['rag_soc']; ?>
      	<br><? echo $acX['sede']; ?></td>
      <td class="<?=$clRF?>" align='left' valign='top'>
			<? echo $acX['cod_sett']; ?></td>
      <td class="<?=$clRF?>" align='left' valign='top'><? echo $sLisBil; ?></td>
      <td class="<?=$clRF?>" align='left' valign='top'><? echo $sLisRat; ?></td>
   </tr><? 
}?>
</table>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
         &nbsp;
      </td>
      <td class="cmd1" align="right">
<?=$sPaginaz?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
